//
//  ZCRankingListVC.m
//  zctx
//
//  Created by apple on 16/2/24.
//  Copyright © 2016年 sanliang. All rights reserved.
//

#import "ZCRankingListVC.h"
#import "ZCEnrollButtonView.H"
#import "ZCRankingCell.h"
#import "ZCRankingListDetailVC.h"
#import "ZCRankingListModelContent.h"
#import "ZCRankingListModelMoreData.h"
@interface ZCRankingListVC ()<UITableViewDelegate,UITableViewDataSource,UITextFieldDelegate>

@property (nonatomic,strong)UITableView *tableView;
@property (nonatomic,strong)UIImageView *imageView;
@property (nonatomic,strong)UITextField *searchView;
@property (nonatomic,strong)UIView *searchContentView;
@property (nonatomic,strong)UIImageView *bgImgView;

@property (nonatomic,strong)NSMutableArray *dataArray; //dataArray
@property (nonatomic,assign)NSInteger offset; //页数
@property (nonatomic,strong)NSString *typeStr;//type
@property (nonatomic,strong)NSString *searchStr; //搜索内容
@property (nonatomic,strong)UIButton *selectButton; //选中Button
@property (nonatomic,assign) NSInteger selectTag;
@end
@implementation ZCRankingListVC


-(void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear: animated];
    theAppdelegate.YJTabBar.hidesBottomBarWhenPushed = YES;
    self.view.shiftHeightAsDodgeViewForMLInputDodger = 50.0f;
    [self.view registerAsDodgeViewForMLInputDodger];
}

-(void)viewDidLoad{
    _offset = 0;
    _typeStr = @"0";
    _searchStr = @"";
    _selectTag = 1;
    self.edgesForExtendedLayout = UIRectEdgeNone;
    self.dataArray = [[NSMutableArray alloc]init];
    
    self.view.backgroundColor = [UIColor colorWithHexString:@"#F1FBFE"];
    [self.view addSubview:self.tableView];
    
    [self.tableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.equalTo(self.view).with.insets(UIEdgeInsetsMake(0, 0, 0, 0));
    }];
    
    [self.tableView registerNib:[UINib nibWithNibName:@"ZCRankingCell" bundle:nil] forCellReuseIdentifier:@"rankingCell"];
    
    self.tableView.mj_footer = [MJDIYFooter footerWithRefreshingTarget:self refreshingAction:@selector(loadMoreData)];
    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
        [self reuquestStudentList:_searchStr];//排行列表
    });
    
}

#pragma mark 加载更多
-(void)loadMoreData{
    //上拉加载
    _offset = _offset+1;
    [self reuquestStudentList:_searchStr];
}

-(void)reuquestStudentList:(NSString *)sarch{
    _searchStr = sarch;
    
    if (_offset==0) {
        NSLog(@"走了这。。");
        [GMDCircleLoader setOnView:self.view withTitle:@"努力加载中..." animated:YES];
    }
    [NetworkManager requestAcsuzhouXwsVotelistmid:UserModel.mid password:UserModel.password pagecount:[NSString stringWithFormat:@"%ld",(long)_offset] keyword:sarch type:self.typeStr success:^(NSDictionary *dictResponse) {
        [GMDCircleLoader hideFromView:self.view animated:YES];
        [self.tableView.mj_footer endRefreshing];
        NSMutableArray *dataRR  = (NSMutableArray *)[dictResponse objectForKey:@"content"];
        if ([dataRR isKindOfClass:[NSMutableArray class]]){
            if (dataRR !=nil && ![dataRR isKindOfClass:[NSNull class]] &&dataRR.count !=0){
                [self.dataArray addObjectsFromArray:dataRR];
                
            }
        }else{
            if (_offset==0) {
            }
            // 拿到当前的上拉刷新控件，变为没有更多数据的状态
            [self.tableView.mj_footer endRefreshingWithNoMoreData];
        }
        NSDictionary *moredict= (NSDictionary *)[dictResponse objectForKey:@"more_data"];
        theAppdelegate.moreModel = [ZCRankingListModelMoreData modelObjectWithDictionary:moredict];
        dispatch_async(dispatch_get_main_queue(), ^{
            [self.tableView reloadData];
        });
        
    } failure:^(NSError *error) {
        [GMDCircleLoader hideFromView:self.view animated:YES];
    }];
}

-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 1;
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.dataArray.count;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    static NSString *cellIndentifier = @"rankingCell";
    ZCRankingCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIndentifier];
    if (cell == nil) {
        cell = [(ZCRankingCell *)[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIndentifier];
    }
    NSDictionary *dict =(NSDictionary *)self.dataArray[indexPath.row];

    cell.ListModel = [ZCRankingListModelContent modelObjectWithDictionary:dict];
    return cell;
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 135;
}

-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return 450;
}

-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    UIView *headView = [[UIView alloc]initWithFrame:CGRectMake(0, 0,SCREEN_WIDTH,450)];
    headView.backgroundColor = [UIColor colorWithHexString:@"#FFFFFF"];
    [headView addSubview:self.imageView];
    [self.imageView makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(headView).with.offset(0);
        make.right.equalTo(headView).with.offset(0);
        make.left.equalTo(headView).with.offset(0);
        make.height.mas_equalTo(@192);
    }];
    
    ZCEnrollButtonView *EnButton = [[ZCEnrollButtonView alloc]initWithFrame:CGRectZero andType:@"0"];
    [headView addSubview:EnButton];
    [EnButton makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.imageView.bottom).with.offset(0);
        make.right.equalTo(headView).with.offset(0);
        make.left.equalTo(headView).with.offset(0);
        make.height.mas_equalTo(@80);
    }];
    EnButton.mModel = theAppdelegate.moreModel;
    EnButton.Ebutton = ^(NSInteger num){
    };
    
    [headView addSubview:self.searchContentView];
    [self.searchContentView makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(EnButton.bottom).with.offset(30);
        make.right.equalTo(headView).with.offset(-19);
        make.left.equalTo(headView).with.offset(19);
        make.height.mas_equalTo(@35);
    }];
    
    [self.searchContentView addSubview:self.searchView];
    [self.searchView makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(self.searchContentView.centerY).with.offset(0);
        make.right.equalTo(self.searchContentView.right).with.offset(-80);
        make.left.equalTo(self.searchContentView.left).with.offset(10);
        make.height.equalTo(self.searchContentView.height).with.offset(0);
    }];
    
    UIButton *searchButton = [[UIButton alloc]initWithFrame:CGRectZero];
    [self.searchContentView addSubview:searchButton];
    [searchButton makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.searchView.top).with.offset(0);
        make.right.equalTo(headView).with.offset(-20);
        make.left.equalTo(self.searchView.right).with.offset(0);
        make.height.mas_equalTo(@35);
    }];
    searchButton.titleLabel.font = [UIFont boldSystemFontOfSize:15];
    searchButton.layer.cornerRadius = 5;
    [searchButton setTitle:@"搜索" forState:UIControlStateNormal];
    [searchButton setTitleColor:[UIColor colorWithHexString:@"#FFFFFF"] forState:UIControlStateNormal];
    searchButton.backgroundColor = [UIColor colorWithHexString:@"#2DBEFF"];
    [searchButton addTarget:self action:@selector(ClickSearchButton) forControlEvents:UIControlEventTouchUpInside];
    
    CGFloat bgimg = SCREEN_WIDTH/6.46;
    [headView addSubview:self.bgImgView];
    [self.bgImgView makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.searchView.bottom).with.offset(0);
        make.right.equalTo(headView).with.offset(0);
        make.left.equalTo(headView).with.offset(0);
        make.height.mas_equalTo(bgimg);
    }];
    UIView *buttonView = [[UIView alloc]init];
    [headView addSubview:buttonView];
    [buttonView makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.bgImgView.bottom).with.offset(0);
        make.bottom.equalTo(headView.bottom).with.offset(0);
        make.right.equalTo(headView).with.offset(0);
        make.left.equalTo(headView).with.offset(0);
    }];
    buttonView.backgroundColor = RGB(241, 251, 253);
    
    UIButton *banButton = [[UIButton alloc]initWithFrame:CGRectZero];
    [buttonView addSubview:banButton];
    [banButton makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(buttonView.top).with.offset(0);
        make.left.equalTo(headView.left).with.offset(19);
        make.height.mas_equalTo(@30);
        make.width.mas_equalTo(@85);
    }];
    banButton.tag = 100;
    banButton.layer.cornerRadius = 4;
    banButton.titleLabel.font = [UIFont systemFontOfSize:15];
    [banButton setTitle:@"班级排名" forState:UIControlStateNormal];
    [banButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    

    [banButton addTarget:self action:@selector(ClickTypeButton:) forControlEvents:UIControlEventTouchUpInside];
//    self.selectButton = banButton;

    UIButton *schoolButton = [[UIButton alloc]initWithFrame:CGRectZero];
    [buttonView addSubview:schoolButton];
    [schoolButton makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(buttonView.top).with.offset(0);
        make.left.equalTo(banButton.right).with.offset(10);
        make.height.mas_equalTo(@30);
        make.width.mas_equalTo(@80);
    }];
    schoolButton.tag = 101;
    schoolButton.layer.cornerRadius = 4;
    schoolButton.titleLabel.font = [UIFont systemFontOfSize:15];
    [schoolButton setTitle:@"学校排名" forState:UIControlStateNormal];
    [schoolButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];

    [schoolButton addTarget:self action:@selector(ClickTypeButton:) forControlEvents:UIControlEventTouchUpInside];
    if (_selectTag==1) {
        banButton.backgroundColor = [UIColor colorWithHexString:@"#2DBEFF"];
        schoolButton.backgroundColor = [UIColor colorWithHexString:@"#2DBEFF" andAlpha:0.5];
    }else{
        banButton.backgroundColor = [UIColor colorWithHexString:@"#2DBEFF" andAlpha:0.5];
        schoolButton.backgroundColor = [UIColor colorWithHexString:@"#2DBEFF"];
    }
    
    return headView;
}

-(UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section{
    return nil;
}

-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    return 0.001;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
    [self performSelector:@selector(unselectCell:) withObject:nil afterDelay:0.3];
    NSDictionary *dict =(NSDictionary *)self.dataArray[indexPath.row];
    ZCRankingListDetailVC *vc = [[ZCRankingListDetailVC alloc] init];
    vc.detailModel = [ZCRankingListModelContent modelObjectWithDictionary:dict];
    vc.hidesBottomBarWhenPushed = YES;
    [self.navigationController pushViewController:vc animated:YES];
}

-(void)unselectCell:(id)sender{
    [self.tableView deselectRowAtIndexPath:[self.tableView indexPathForSelectedRow] animated:YES];
}

-(BOOL)textFieldShouldReturn:(UITextField *)textField{
    [textField resignFirstResponder];
    return YES;
}

#pragma mark 点击搜索按钮
-(void)ClickSearchButton{
    [self.searchView resignFirstResponder];
    if (![self.searchView.text isEqualToString:_searchStr]) {
        [self.dataArray removeAllObjects];
        [self reuquestStudentList:self.searchView.text];
    }
}

#pragma mark 排名 班级 =0 ；学校 =1
-(void)ClickTypeButton:(UIButton *)sender{
    [self.dataArray removeAllObjects];
    
    if (self.selectButton !=sender) {
        _offset = 0;
        if (sender.tag == 100) {
            self.typeStr = @"0";//班级排名
            _selectTag = 1;
        }else{
            self.typeStr = @"1";//学校排名
            _selectTag = 2;
        }
        self.selectButton = sender;
        [self reuquestStudentList:self.searchView.text];
    }
}


-(UITableView *)tableView{
    if (!_tableView) {
        _tableView = [[UITableView alloc]initWithFrame:CGRectZero style:UITableViewStyleGrouped];
        _tableView.delegate = self;
        _tableView.dataSource = self;
        _tableView.tableFooterView = [[UIView alloc]init];
        _tableView.sectionFooterHeight = 0.001;
        _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        _tableView.backgroundColor = [UIColor colorWithHexString:@"#F1FBFE"];
    }
    return _tableView;
}

-(UIImageView *)imageView{
    if (!_imageView) {
        _imageView = [[UIImageView alloc]initWithFrame:CGRectZero];
        _imageView.image = [UIImage imageNamed:@"enroll_head"];
    }
    return _imageView;
}

-(UITextField *)searchView{
    if (!_searchView) {
        _searchView = [[UITextField alloc] init];
        _searchView.delegate = self;
        _searchView.borderStyle = UITextBorderStyleNone;
        _searchView.clearsContextBeforeDrawing = YES;
        _searchView.placeholder = @"输入参赛姓名或编号";
        _searchView.font = [UIFont systemFontOfSize:14];
        _searchView.textColor = [UIColor colorWithHexString:@"#8A8A8A"];
    }
    return _searchView;
}

-(UIView *)searchContentView{
    if (!_searchContentView) {
        _searchContentView = [[UIView alloc]init];
        _searchContentView.layer.borderWidth =1;
        _searchContentView.layer.cornerRadius = 5;
        _searchContentView.layer.borderColor = [UIColor colorWithHexString:@"#2DBEFF"].CGColor;
        _searchContentView.backgroundColor = [UIColor colorWithHexString:@"#FFFFFF"];

    }
    return _searchContentView;
}

-(UIImageView *)bgImgView{
    if (!_bgImgView) {
        _bgImgView = [[UIImageView alloc]init];
        _bgImgView.image = [UIImage imageNamed:@"enroll_lang"];
        _bgImgView.backgroundColor = RGB(241, 251, 253);
        //        _bgImgView.backgroundColor = [UIColor orangeColor];
    }
    return _bgImgView;
}

@end
